package com.vf.admin.client.common;

import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.fiwork.dto.UserInfoDto;
import com.vf.admin.fiwork.util.SpringBeanUtilsUtil;
import com.vf.admin.miaodong.entity.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.vf.admin.common.Constants.code_return_sucess;

@Slf4j
public class ClientBaseController {
    @Autowired
    public HttpServletRequest request;

    @Autowired
    public HttpServletResponse response;

    @Autowired
    public RedisUtil redisUtil;

    public Result result = new Result();

    public Integer getUserId() {
        return (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
    }

    /**
     * 成功 分页
     **/
    public Result sucessPage(Object date,Object page){
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setMsg("success");
        result.setData(date);
        result.setPage(page);
        return result;
    }



    public Result sucessJSON(Object data){
        Result result = new Result();
        result.setCode(Constants.code_return_sucess);
        result.setData(data);
        return result;
    }

    public Result sucessJSON(String msg){
        Result result = new Result();
        result.setData("");
        result.setCode(Constants.code_return_sucess);
        result.setMsg(msg);
        return result;
    }

    public static Result successJSON(){
        Result result = new Result();
        result.setData("");
        result.setCode(Constants.code_return_sucess);
        result.setMsg("success");
        return result;
    }

    /**
     * 没有上传过
     * @return
     */
    public Result sucessNoUploadJSON(){
        Result result = new Result();
        result.setCode(Constants.code_upload_bucunzai);
        return result;
    }

    /**
     * 已经上传过了
     * @return
     */
    public Result sucessIsUploadJSON(){
        Result result = new Result();
        result.setCode(Constants.code_upload_yicunzai);
        return result;
    }

    /**
     * 上传了一部分
     * @param data
     * @return
     */
    public Result sucessIngUploadJSON(Object data){
        Result result = new Result();
        result.setCode(Constants.code_upload_yibufen);
        result.setData(data);
        return result;
    }


    /**
     * 业务状态不正常，客户端提示一下即可
     * @param msg
     * @return
     */
    public Result errorJSON(String msg){
        Result result = new Result();
        //result.setData("");
        result.setCode(Constants.code_return_error);
        result.setMsg(msg);
        return result;
    }

    /**
     * 业务状态不正常，需要客户端进行单独处理该提示
     * @param msg
     * @return
     */
    public Result errorCLJSON(String msg){
        Result result = new Result();
        result.setCode(Constants.code_return_error_CL);
        result.setMsg(msg);
        return result;
    }

    /**
     * 业务状态不正常，需要客户端进行单独处理该提示
     * @param msg
     * @return
     */
    public Result errorCLJSON(Object data,String msg){
        Result result = new Result();
        result.setCode(Constants.code_return_error_CL);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public Result errorJSON(Object data,String msg){
        Result result = new Result();
        result.setCode(Constants.code_return_error);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public Result sucessJSON(Object data,String msg){
        Result result = new Result();
        result.setCode(Constants.code_return_sucess);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /***
     * 使用Ajax返回结果时的简化处理
     * @param data
     * @throws IOException
     */
    private void toJSON(Object data) throws IOException {
        this.response.setCharacterEncoding("utf-8"); //用utf-8转换获得传输过来的码
        this.response.setContentType("application/json");
        PrintWriter out = this.response.getWriter();
        out.print(data);
        out.flush();
        out.close();
    }


    public Result result(int code, String msg, Object data){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public Result result(int code, String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 过滤用户信息的实体
     * @param user
     * @return
     */
    public UserInfoDto userInfo(AppUser user){
        UserInfoDto userInfoDto = new UserInfoDto();
        SpringBeanUtilsUtil.copyPropertiesIgnoreNull(user,userInfoDto);
        return userInfoDto;
    }

    public String sucess(String url){
        return url;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

}
